<?php

class User_AuthController extends User_Library_Controller_Action
{

    /**
     * @todo redirect to login page
     * @return void
     */
    public function indexAction()
    {
        $this->_redirect('/user/auth/login');
    }

    /**
     * @todo Authentication
     * valid: redirect to default administrator site.
     * invalid: alert error message.
     *
     * @return void
     */
    public function loginAction()
    {
        // redirect to user profile if user loged-in
        if (!empty($this->view->userProfile)) {
            $this->_redirect('/user/account/profile');
        }

        // Start
        $request = $this->getRequest();

        // 1. Render form
        $form = new User_Form_Login();
        $form->getElement('csrf_token')->removeValidator('Identical');

        // 2. submit form
        if ($request->isPost() && $form->isValid($request->getPost())) {
            $values = array();
            $values['email'] = $request->getParam('email');
            $values['password'] = $request->getParam('password');
            // 2.2  login process
            if ($this->process($values)) {
                $urlOptions = array(
                    'module' => 'default',
                    'controller' => 'index',
                    'action' => 'index'
                );
                $this->_helper->redirector->gotoRoute($urlOptions);
            } else {
                $this->view->msg = 'username-or-password-not-match';
            }
        }

        $this->view->form = $form;
    }

    /**
     * @todo clear authenticated session
     * @return void
     */
    public function logoutAction()
    {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        if(isset($_SESSION['KCFINDER'])) {
            unset($_SESSION['KCFINDER']);
        }
        $urlOptions = array(
            'module' => 'user',
            'controller' => 'auth',
            'action' => 'login'
        );
        $this->_helper->redirector->gotoRoute($urlOptions);
    }

    /**
     * @todo Login process
     * @param Array $values
     * @return boolean
     */
    private function process($values)
    {
        $result = false;
        $modelUser = new Model_User();
        $check = $modelUser->userLoginFont($values);
        if ($check) {
            $check['is_admin'] = $modelUser->isAdmin($check['id']);
            $userRole = $modelUser->getRoleUser($check['id']);
            $role_name = 'Guest';
            $role_id = 5;
            if(isset($userRole ['role_name']) && isset($userRole ['role_id'])) {
                $role_name = $userRole ['role_name'];
                $check['role_name'] = $role_name;
                $check['role_id'] = $userRole ['role_id'];
            }
            $resourceObj = new Model_Entities_AclResource();
            $wheres = array('status = 1');
            if($check['role_id'] == 1) {
                $wheres = array();
            }
            $check['resources'] = $resourceObj->getArray(0, 0, $wheres, array('id'));
            Zend_Auth::getInstance()->getStorage('FO')->write($check);
            $result = true;
        }
        return $result;
    }

}